﻿using Bookie.Model;
using Bookie.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Bookie.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class HomePage : Page
    {
        public HomePage()
        {
            this.InitializeComponent();
            StatusBar statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
            statusBar.HideAsync();
            showWebViewOffer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += _HardwareButtons_BackPressed;
            this.Loaded += HomePage_Loaded;
        }

        void HomePage_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(4.0);
            _timer.Tick += ((senders, es) =>
            {
                if (loadBanner.SelectedIndex < loadBanner.Items.Count - 1)
                    loadBanner.SelectedIndex++;
                else
                    loadBanner.SelectedIndex = 0;
            });
            _timer.Start();
        }

        private void _HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            if (showWebViewOffer.Visibility== Windows.UI.Xaml.Visibility.Visible)
            {
                showWebViewOffer.NavigateToString("<html></html>");
                showWebViewOffer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        public HomeViewModel HomeViewModel
        {
            get
            {
                return (HomeViewModel)DataContext;
            }
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        private void Bookies_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Favourites_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Offers_Click(object sender, RoutedEventArgs e)
        {

        }

        private void SelectItemClick(object sender, ItemClickEventArgs e)
        {
            HomeViewModel.IsShowLoading = true;
            var getData = (Offer)e.ClickedItem;
            showWebViewOffer.Visibility = Windows.UI.Xaml.Visibility.Visible;
            showWebViewOffer.Source = new Uri(getData.link, UriKind.Absolute);
        }

        private void loadComplete(object sender, NavigationEventArgs e)
        {
            HomeViewModel.IsShowLoading = false;
        }

        private void startLoad(WebView sender, WebViewNavigationStartingEventArgs args)
        {
            HomeViewModel.IsShowLoading = true;
        }

        private void selectDataBannner(object sender, TappedRoutedEventArgs e)
        {
            HomeViewModel.IsShowLoading = true;
            var getData = (FlipView)sender;
            var data = getData.SelectedItem as Banner;
            showWebViewOffer.Visibility = Windows.UI.Xaml.Visibility.Visible;
            showWebViewOffer.Source = new Uri(data.link, UriKind.Absolute);
        }

    }
}
